package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.mapper.dishMapper;
import com.sky.service.WorkSpaceSrevice;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkSpaceServicelmpl implements WorkSpaceSrevice {
    @Autowired
  private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private dishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO getbusinessData(LocalDateTime begin, LocalDateTime end) {
        //营业额 select count(amount) from orders where statue=5 and order_time > begin and order_time < end;
        Map map =new HashMap<>();
        map.put("status",5);
        map.put("begin",begin);
        map.put("end",end);
        Double turnover = orderMapper.selectStatus(map);

        //新增用户数 select  count(id) from user where create_time >begin and create_time <end
        Integer useUsers = userMapper.selectNewCreateTime(begin, end);

        //有效订单 select count(id) from orders where statue=5 and order_time > begin and order_time < end;
        Integer orderCount = getOrderCount(begin, end, 5);

        //总订单 select count(id) from orders where  order_time > begin and order_time < end;
        Integer allOrderCount = getOrderCount(begin, end, null);

        //订单完成率 ：有效订单/总订单 *100%
        Double orderCompletionRate = orderCount.doubleValue() / allOrderCount;

        //平均客单价：营业额/有效订单数
        double unitPrice = turnover / orderCount;

        return BusinessDataVO.builder()
                .turnover(turnover)
                .newUsers(useUsers)
                .validOrderCount(orderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .build();
    }

    /**
     * 根据条件统计订单数量
     * @param begin
     * @param end
     * @param status
     * @return
     */
    private Integer getOrderCount(LocalDateTime begin, LocalDateTime end ,Integer status){
        Map map=new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status",status);

        return orderMapper.selectOrder(map);
    }

    /**
     * 查询订单管理数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderOverViewVO getoverviewOrders(LocalDateTime begin, LocalDateTime end) {
        //全部订单 select count(id) from orders where order_time > begin and order_time < end;
        Integer allOrderCount = getOrderCount(begin, end, null);

        //已取消数量 select count(id) from orders where status = ? order_time > begin and order_time < end;
        Integer cancelledOrders = getOrderCount(begin, end, Orders.CANCELLED);

        //已完成数量  select count(id) from orders where status = ? order_time > begin and order_time < end;
        Integer completedOrders = getOrderCount(begin, end, Orders.COMPLETED);

        //待派送数量  select count(id) from orders where status = ? order_time > begin and order_time < end;
        Integer deliveredOrders = getOrderCount(begin, end, Orders.CONFIRMED);

        //待接单数量select count(id) from orders where status = ? order_time > begin and order_time < end;
        Integer waitingOrders = getOrderCount(begin, end, Orders.TO_BE_CONFIRMED);

        return OrderOverViewVO.builder()
                .allOrders(allOrderCount)
                .cancelledOrders(cancelledOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .completedOrders(completedOrders)
                .build();
    }

    /**
     * 查询菜品总览
     * @param
     * @param
     * @return
     */
    @Override
    public DishOverViewVO getoverviewDishes() {
        //已停售菜品数量 select count(id) from dish where status = 0
       Integer discontinued = dishMapper.getStatus(0);
        //已启售菜品数量
        Integer sold = dishMapper.getStatus(1);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public DishOverViewVO getoverviewSetmeals() {
        //已停售套餐数量 select count(id) from dish where status = 0
        Integer discontinued = setmealMapper.getStatus(0);
        //已启售套餐数量
        Integer sold = setmealMapper.getStatus(1);
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

}

